package br.edu.ufcg.embedded.eolica.view;

import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;

import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.TextField;

import br.edu.ufcg.embedded.eolica.MIDlet.UsinaEolicaMIDlet;
import br.edu.ufcg.embedded.eolica.domain.User;
import br.edu.ufcg.embedded.eolica.exception.FacadeException;
import br.edu.ufcg.embedded.eolica.facade.Facade;
import br.edu.ufcg.embedded.eolica.util.MyCallback;
import br.edu.ufcg.embedded.eolica.util.Properties;

public class ConsultForm extends Form implements CommandListener {
	
	private Displayable previous;
	private Command cmdNext, cmdBack;
	private Display d;
	private Properties prop;
	private String graphType;
	private ChoiceGroup choiceStations;
	private ConsultFormAtt consultAtt;
	
	
	public ConsultForm(Display d, Displayable previous, String consult) {
		super("Eletrobras - " + consult);
		this.d = d;
		this.previous = previous;		
		
		loadMap(); 
		loadComponents();
	}
	
	
	private void loadMap() {
		InputStream input = getClass().getResourceAsStream("/mapa-paraiba.png");
		byte[] imagebyte;
		try {
			imagebyte = new byte[input.available()];
			input.read(imagebyte);
			append(Image.createImage(imagebyte, 0, imagebyte.length));
		} catch (IOException e) {
			d.setCurrent(new ErrorAlert(prop.getProperty("label.error"), prop.getProperty("error.internal")), previous);
		} catch (Exception e) {
			d.setCurrent(new ErrorAlert(prop.getProperty("label.error"), prop.getProperty("error.internal")), previous);
		}
	}

	private void loadComponents() {
		prop = UsinaEolicaMIDlet.getProp();
		cmdNext = new Command(prop.getProperty("label.next"), Command.OK, 0);
		cmdBack = new Command(prop.getProperty("label.back"), Command.BACK, 0);
		
		String[] stationNames = new String[]{"419", "425", "427", "443", "453"};
		choiceStations = new ChoiceGroup(prop.getProperty("label.stationList"), Choice.EXCLUSIVE, stationNames, null);
		
		append(choiceStations);
		addCommand(cmdNext);
		addCommand(cmdBack);
		setCommandListener(this);
	}

	public void commandAction(Command cmd, Displayable arg1) {
		if (cmd == cmdBack)
			d.setCurrent(previous);
		else if(cmd == cmdNext) {
			if(consultAtt == null)
				consultAtt = new ConsultFormAtt(d, this, prop.getProperty("label.graphAtt"));
			
			consultAtt.setSelectedStation(choiceStations.getString(choiceStations.getSelectedIndex()));
			//consultAtt.setGraphType(graphType);
			d.setCurrent(consultAtt);
		}
	}
	
}
